import java.util.Arrays;

public class Test {
    //用方法使一个数组元素奇数在前，偶数在后
    public static void func(int[] array){
        int i = 0;
        int j = array.length;
        while(i<j){
            while(i<j && array[i]%2 != 0){
                i++;
            }
            while(i<j && array[j]%2 == 0){
                j--;
            }
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }
    //用transform实现将数组每项扩大一倍
    public static int[] transform(int[] array){
        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = array[i]*2;
        }
        return tmp;
    }

    //冒泡排序
    public static void bublesort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            //优化 - 检查某一趟是否有序
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                int tmp = array[j];
                array[j] = array[j+1];
                array[j+1] = tmp;
                flg = true;//证明发生交换
            }
            if(flg == false){
                return;//证明已经有序
            }
        }
    }
    //降序
    public static void reverse(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left<right){
            int tmp = left;
            left = right;
            right = tmp;
            left++;
            right--;
        }
    }
    //二分查找
    public static int binarysearch(int[] array, int key){
        int left = 0;
        int right = array.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(array[mid]<key){
                left = mid+1;
            } else if (array[mid]>key) {
                right = mid-1;
            }else {
                return mid;
            }
        }
        //查找的数不存在
        return -1;
    }
    //找数组某一位
    public static int find(int[] array,int key){
        for (int i = 0; i < array.length; i++) {
            if(array[i]==key){
                return i;
            }
        }
        return -1;
    }
    //数组求平均值
    public static double avg(int[] array){
        int sum = 0;
        for (int x:
             array) {
            sum += x;
        }
        return sum*1.0/array.length;
    }
    public static void main(String[] args) {
        int[] array = {1,2,4,3,5,8,7,6,10,9};
        System.out.println(avg(array));
        System.out.println(find(array,10));
        Arrays.sort(array);//底层排序，快排
        System.out.println(binarysearch(array,6));

        int[] array0 = {1,2,4,3,5,8,7,6,10,9};
        Arrays.equals(array0,array);//数组比较 - boolean类型
        Arrays.fill(array0,-1);//全部初始化为某个值
        Arrays.fill(array0,2,5,-1);//区间内初始化
    }
    //汉诺塔
    /*
    * pos1 - 起始位置
    * pos2 - 中转位置
    * pos3 - 目标位置
    * n - 移几个盘子
    * */
    public static void hanic(int n,char pos1,char pos2,char pos3){
        if(n == 1){
            move(pos1,pos3);
        }
        hanic(n-1,pos1,pos3,pos2);
        move(pos1,pos3);
        hanic(n-1,pos2,pos1,pos3);
    }
    public static void move(char pos0,char pos1){
        //由哪移向哪
        System.out.println(pos0 + "->" + pos1);
    }

    public static void main1(String[] args) {
        hanic(1,'A','B','C');
        System.out.println();
        hanic(2,'A','B','C');
        System.out.println();
        hanic(3,'A','B','C');

    }


    public static int add(int a,int b){
        //System.out.println(a+b);
        return a+b;
    }

    public static void main0(String[] args) {
        int a=1,b=2;
        add(a,b);
    }
}
